package com.jiaoxiaobo.queue;

import com.jiaoxiaobo.RabbitMQUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class ClassQueueSender {

    private static final String QUEUE_NAME = "hello-class-queue";

    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtil.getConnection();
        Channel channel = connection.createChannel();
        //durable,是否持久化到磁盘
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        String message = "Hello World!333";

        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
        builder.deliveryMode(MessageProperties.PERSISTENT_TEXT_PLAIN.getDeliveryMode());
        builder.priority(MessageProperties.PERSISTENT_TEXT_PLAIN.getPriority());

        //携带消息id
        builder.messageId(""+channel.getNextPublishSeqNo());
        Map<String, Object> headers = new HashMap<>();
        //携带订单号
        headers.put("order", "123");
        builder.headers(headers);

        channel.basicPublish("",QUEUE_NAME,builder.build(),message.getBytes(StandardCharsets.UTF_8));

        System.out.println(" [x] Sent '" + message + "'");

        channel.close();
        connection.close();

    }

}
